/**
 * @class   wosh::gui::WidgetQMenu
 * @brief   Custom QMenu implementation with (auto) refresh features.
 *          Useful to show dynamic items such as bundles and devices.
 *
 ****************************************************************************
 * @version $Id: WidgetQMenu.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_WidgetQActionBundle_H__
 #define __WOSH_Gui_WidgetQActionBundle_H__

 #include <framework/gui/IWidget.h>
 #include <framework/gui/WoshObject.h>
 #include <framework/gui/GuiUtilities.h>
 #include <core/Table.h>
 #include <core/List.h>
 #include <QtGui/QMenu>


namespace wosh {
 namespace gui {


class WidgetQMenuItem :  public QAction, public WoshObject  {

	public:

		WidgetQMenuItem(QObject* parent = 0) : QAction(parent), WoshObject() { }
		WidgetQMenuItem(const QString &text, QObject* parent = 0) : QAction(text, parent), WoshObject() { }

		virtual ~WidgetQMenuItem() { }


	public:

		virtual void updateStatusTip() {
			QString tip = QString("%1 [%2] {%3} : %4")
				.arg(this->getRemoteName().c_str())
				.arg(this->getRemoteType().c_str())
				.arg(this->getInterfaces().join("; "))
				.arg(this->getRemoteURI().toString().c_str());

			this->setToolTip( tip );
			this->setStatusTip( tip );
		 }


	public:

		virtual void setRemoteName( const std::string& name ) {
			WoshObject::setRemoteName(name);
			this->setText( name.c_str());
			updateStatusTip();
		 }

		virtual void setRemoteType( const std::string& type ) {
			WoshObject::setRemoteType(type);
			this->setIcon(GuiUtilities::createIconFor(type));
			updateStatusTip();
		 }


	protected:



}; // class def




class WidgetQMenu :		public QMenu,
						public IWidget
 {
	Q_OBJECT

	WOSH_CLASSNAME(wosh::gui::WidgetQMenu)
	WOSH_CLASS_ABSTRACT(false)


	public:
		WidgetQMenu( QWidget *parent = NULL );

		virtual ~WidgetQMenu();


		virtual QWidget* asQWidget()		{ return this; }

	public:

		virtual void addSeparator();
		virtual void addAction( QAction* action );

		virtual void clear();

		virtual void addActionDynamic( WidgetQMenuItem * action );

		virtual void setParsingData( bool value );

	public:

		inline void setDefaultType( const std::string& value )	{ this->defaultType = value; }
		inline void setForceType( bool value )					{ this->forceType = value; }

		inline void setRequestMethod( const std::string& value ) { this->requestMethod = value; }
		inline void setRequestArgument( ISerializable* data )	{ this->requestArgument = data; }

		inline void setRequestTimeOut( unsigned int seconds )	{ this->requestTimeOutSec = seconds; }


		inline std::string& getRequestMethod()					{ return this->requestMethod; }
		inline const std::string& getRequestMethod() const		{ return this->requestMethod; }

		inline unsigned int getRequestTimeOut()					{ return this->requestTimeOutSec; }


		inline std::string& getDefaultType()					{ return this->defaultType; }
		inline const std::string& getDefaultType() const		{ return this->defaultType; }

		inline bool getForceType()								{ return this->forceType; }


		inline QList<WidgetQMenuItem*>& getActionsDynamic()		{ return this->actionDynamic; }


	public:

		void parse( const ISerializable* obj );
		void parseTable( const wosh::Table* table );
		void parseList( const wosh::List* dList );

	public:
		virtual void busMessage( const Message& message, const Bus* source );


	public slots:

		virtual void refreshAll();

	protected slots:

		void request_timedout();

		void event_triggered( QAction* action );


	signals:
		void triggeredItem( WidgetQMenuItem* item );
		void updated();


	protected:
		virtual void clearDynamics();

	protected:
		void changeEvent(QEvent *e);

	protected:
		std::string defaultType;
		bool forceType;

		std::string requestMethod;
		ISerializable* requestArgument;

		unsigned int requestTimeOutSec;

		bool joinSources;

		QList<WidgetQMenuItem*> actionDynamic;

		QAction* actionNoContents;
		QAction* actionRefresh;
		bool showItemSeparator;


}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_WidgetQActionBundle_H__
